from fastapi import FastAPI, Request
from fastapi.staticfiles import StaticFiles
from core import Router, Events
from config import settings
from fastapi.middleware.cors import CORSMiddleware

"""
1. pip install fastapi
2. pip install uvicorn
3. pip install tortoise-orm
4. pip install aiomysql
5. pip install jinja2
6. pip install python-multipart
7. pip install pyjwt
8. pip install requests
9. pip install python-dotenv
10. pip install oss2
11. pip install pillow
12. pip install xmltodict
13. pip install aioredis
"""

app = FastAPI()


# 监听事件
app.add_event_handler('startup', Events.start(app))
app.add_event_handler('shutdown', Events.stop)


# 中间件 middleware
@app.middleware('http')
async def custom_middleware(request: Request, call_next):
    print('接口被调用前执行，客户ip', request.client.host)
    response = await call_next(request)
    print('接口被调用并返回结果后执行，客户ip', request.client.host)
    return response


# 允许所有来源的请求
app.add_middleware(
    CORSMiddleware,
    allow_origins=["*"],
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)

# 路由
app.include_router(router=Router.router)


# 静态资源目录
app.mount(path='/', app=StaticFiles(directory=settings.STATIC_DIR))


if __name__ == '__main__':
    import uvicorn
    uvicorn.run(app='main:app', host='localhost', port=8088, reload=True)

